<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" label-width="80px" class="login-form">
      <h2 class="login-title">仓库管理系统登录</h2>
      <el-form-item label="账号：" prop="account">
        <el-input v-model="loginForm.account" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="角色：" prop="role">
        <el-radio-group v-model="loginForm.role">
          <el-radio label="admin">管理员</el-radio>
          <el-radio label="employee">员工</el-radio>
          <el-radio label="user">用户</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="login-button">
        <el-button :loading="loading" type="primary" @click="login">登录</el-button>
        <!-- 用户注册按钮 -->
        <el-button type="success" @click="openRegisterDialog">注册</el-button>
      </el-form-item>
    </el-form>
  </div>

  <!-- 注册弹窗 -->
  <el-dialog v-model="dialogFormVisible" title="用户注册" @close="resetRegisterForm">
    <el-form :model="registerForm" ref="registerForm" label-width="80px">
      <el-form-item label="账号" prop="account">
        <el-input v-model="registerForm.account" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="registerForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="registerForm.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="registerForm.age" placeholder="请输入年龄"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="registerForm.phonenumber" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <!-- 角色固定为用户，不可修改 -->
      <el-form-item label="角色" prop="role">
        <el-input v-model="registerForm.role" disabled placeholder="角色为用户" />
      </el-form-item>

      <div slot="footer" class="dialog-footer">
        <el-button @click="resetRegisterForm">取消</el-button>
        <el-button type="primary" @click="register">注册</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: '',
        role: 'admin' // 默认角色为管理员
      },
      loading: false,
      isLoggingIn: false,
      dialogFormVisible: false, // 控制注册弹窗的显示与隐藏
      registerForm: {
        account: '',
        password: '',
        name: '',
        gender: '',
        age: '',
        phonenumber: '',
        role: '用户' // 默认角色为用户
      }
    };
  },
  methods: {
    async login() {
      if (this.isLoggingIn) return;
      this.isLoggingIn = true;

      if (!this.loginForm.account || !this.loginForm.password) {
        this.$message.error('账号和密码不能为空');
        this.isLoggingIn = false;
        return;
      }

      const roleUrlMap = {
        admin: 'http://localhost:8080/login/admin',
        employee: 'http://localhost:8080/login/employee',
        user: 'http://localhost:8080/login/user'
      };

      const url = roleUrlMap[this.loginForm.role];
      if (!url) {
        this.$message.error('请选择角色');
        this.isLoggingIn = false;
        return;
      }

      this.loading = true;
      try {
        const response = await axios.post(url, {
          account: this.loginForm.account,
          password: this.loginForm.password
        });
        if (response.data.code === 1) {
          const name = response.data.data.name;
          this.$message.success('登录成功，欢迎 ' + name);
          // 触发 loginSuccess 事件，传递登录信息
          this.$emit('loginSuccess', { account: this.loginForm.account, role: this.loginForm.role, name: name });
        } else {
          this.$message.error(response.data.msg);
        }
      } catch (error) {
        console.error('登录请求失败', error);
        this.$message.error('登录请求失败');
      } finally {
        this.loading = false;
        this.isLoggingIn = false;
      }
    },
    openRegisterDialog() {
      this.dialogFormVisible = true; // 打开注册弹窗
    },
    resetRegisterForm() {
      this.registerForm = {
        account: '',
        password: '',
        name: '',
        gender: '',
        age: '',
        phonenumber: '',
        role: '用户' // 默认角色为用户
      };
      // 关闭弹窗
      this.dialogFormVisible = false;
    },
    async register() {
      if (!this.registerForm.account || !this.registerForm.password || !this.registerForm.name || !this.registerForm.gender || !this.registerForm.age || !this.registerForm.phonenumber) {
        this.$message.error('请填写完整的注册信息');
        return;
      }

      try {
        const response = await axios.post('http://localhost:8080/user/register', this.registerForm);
        if (response.data.code === 1) {
          this.$message.success('注册成功');
          this.dialogFormVisible = false; // 注册成功后关闭弹窗
        } else {
          this.$message.error(response.data.msg);
        }
      } catch (error) {
        console.error('注册请求失败', error);
        this.$message.error('注册请求失败');
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-image: url('src/images/1.png'); /* 替换为你的背景图片路径 */
  background-size: cover;
  background-position: center;
}

.login-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
}

.login-form {
  width: 400px;
  background-color: #f0f2f5;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-button {
  display: flex;
  justify-content: center;
  margin-left: 60px;
}
</style>